package com.jhgsys.internal.accident.service.impl;

import com.jhgsys.internal.accident.entity.AccidentAuditRecord;
import com.jhgsys.internal.accident.mapper.AccidentAuditRecordMapper;
import com.jhgsys.internal.accident.service.IAccidentAuditRecordService;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.FebsUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.StringPool;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 事故上报审核记录 Service实现
 *
 * @author admin
 * @date 2024-08-07 16:24:25
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class AccidentAuditRecordServiceImpl extends ServiceImpl<AccidentAuditRecordMapper, AccidentAuditRecord> implements IAccidentAuditRecordService {

    /**
     * 查询（分页）
     * @author admin
     * @date 2024-08-07 16:24:25
     * @param request QueryRequest
     * @param accidentAuditRecord accidentAuditRecord
     * @return IPage<AccidentAuditRecord>
    */
    @Override
    public IPage<AccidentAuditRecord> findAccidentAuditRecords(QueryRequest request, AccidentAuditRecord accidentAuditRecord) {
        LambdaQueryWrapper<AccidentAuditRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AccidentAuditRecord::getDeletedFlg, 0);
        // TODO 设置查询条件

        Page<AccidentAuditRecord> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);
        return this.page(page, queryWrapper);
    }

    /**
     * 查询（所有）
     * @author admin
     * @date 2024-08-07 16:24:25
     * @param accidentAuditRecord accidentAuditRecord
     * @return List<AccidentAuditRecord>
     */
    @Override
    public List<AccidentAuditRecord> findAccidentAuditRecords(AccidentAuditRecord accidentAuditRecord) {
	    LambdaQueryWrapper<AccidentAuditRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AccidentAuditRecord::getDeletedFlg, 0);
		// TODO 设置查询条件
		return this.baseMapper.selectList(queryWrapper);
    }

    /**
    * 根据ID查询
    * @author admin
    * @date 2024-08-07 16:24:25
    * @param id
    * @return AccidentAuditRecord
    */
    @Override
    public AccidentAuditRecord findAccidentAuditRecordById(Integer id){
        return this.baseMapper.selectById(id);
    }

    /**
     * 新增
     * @author admin
     * @date 2024-08-07 16:24:25
     * @param accidentAuditRecord accidentAuditRecord
     */
    @Override
    @Transactional
    public void createAccidentAuditRecord(AccidentAuditRecord accidentAuditRecord) {
        accidentAuditRecord.setCreatedTime(new Date());
        this.save(accidentAuditRecord);
    }

    /**
     * 修改
     * @author admin
     * @date 2024-08-07 16:24:25
     * @param accidentAuditRecord accidentAuditRecord
     */
    @Override
    @Transactional
    public void updateAccidentAuditRecord(AccidentAuditRecord accidentAuditRecord) {
        accidentAuditRecord.setUpdatedTime(new Date());
        this.saveOrUpdate(accidentAuditRecord);
    }

    /**
     * 逻辑删除
     * @author admin
     * @date 2024-08-07 16:24:25
     * @param accidentAuditRecord accidentAuditRecord
     * @param accidentAuditRecordIds accidentAuditRecordIds
     */
    @Override
    @Transactional
    public void deleteAccidentAuditRecord(AccidentAuditRecord accidentAuditRecord,String accidentAuditRecordIds) {
        List<String> list = Arrays.asList(accidentAuditRecordIds.split(StringPool.COMMA));
        this.baseMapper.update(accidentAuditRecord,new LambdaQueryWrapper<AccidentAuditRecord>().in(AccidentAuditRecord::getAuditRecordId,list));
	}

    /**
    * 物理删除
    * @author admin
    * @date 2024-08-07 16:24:25
    * @param accidentAuditRecordIds accidentAuditRecordIds
    */
    @Override
    @Transactional
    public void physicallyDeleteAccidentAuditRecord(String accidentAuditRecordIds) {
        List<String> list = Arrays.asList(accidentAuditRecordIds.split(StringPool.COMMA));
        LambdaQueryWrapper<AccidentAuditRecord> wapper = new LambdaQueryWrapper<>();
        wapper.in(AccidentAuditRecord::getAuditRecordId,list);
        this.remove(wapper);
    }
}
